package com.xii.eduorder.controller;


import com.xii.commonutils.R;
import com.xii.eduorder.service.TPayLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 生成微信支付二维码
 * </p>
 *
 * @author xhjava
 * @since 2022-03-22
 */

@RestController
@RequestMapping("/orderservice/log")
//@CrossOrigin
public class TPayLogController {
    @Autowired
    private TPayLogService tpayService;
    /**
     * 生成二维码
     *
     * @return
     */
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable String orderNo) {
        Map map = tpayService.createNative(orderNo);
        return R.ok().data(map);
    }

    /**
     * @description: 获取支付状态
     *
     * @return {@link null}
     * @author  wangxihao
     * @email wangxh0108@163.com
    **/
    @GetMapping("/queryPayStatus/{orderNo}")
    public R queryPayStatus(@PathVariable String orderNo) {
        //调用查询接口
        Map<String, String> map = tpayService.queryPayStatus(orderNo);
        System.out.println("查询订单："+map);
        if (map == null) {//出错
            return R.error().message("支付出错");
        }
        if (map.get("trade_state").equals("SUCCESS")) {//固定值  如果成功 trade_state会为success
            //更改订单状态   并加入付款记录表
            tpayService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}


